package com.jxtc.enterprise.admin.vo;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.jxtc.enterprise.common.constants.FoodStatus;
import com.jxtc.enterprise.common.entity.Food;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.Objects;

@Schema(description = "Admin 端菜品信息项")
@Getter
@Setter
@ToString
public class FoodVO {
    @Schema(description = "菜品 ID", example = "698ee133abc5a355ed3ced1607f6eb3c")
    private String foodId;

    @Schema(description = "菜品名称", example = "辣子鸡")
    private String foodName;

    @Schema(description = "菜品图片链接", example = "https://yong-shan-tuan.oss-cn-hangzhou.aliyuncs.com/60459f52-8db3-41b0-8a40-2f96cf7cf375.jpg")
    private String foodImage;

    @Schema(description = "菜品描述信息", example = "这是一个菜品描述信息")
    private String description;

    @Schema(description = "菜品规格信息", example = "[{\"type\":\"一级规格 2\",\"items\":[{\"storePrice\":12.8,\"salePrice\":13.8,\"name\":\"二级规格\",\"limitCount\":10}]}]")
    private JSONArray specification;

    @Schema(description = "店铺 ID", example = "785415ff340c804dc9ec91a7cab40bc3")
    private String storeId;

    @Schema(description = "店铺名称", example = "深食堂-湘式小炒")
    private String storeName;

    // @Schema(description = "出售价，单位为：元", example = "15")
    // private Double salePrice;

    @Schema(description = "商家合作价，单位为：元", example = "15")
    private Double storePrice;

    @Schema(description = "限购份数", example = "0")
    private Integer limitCount;

    @Schema(description = "菜品在店铺内的分类名称", example = "单独小炒")
    private String foodTypeName;

    @Schema(description = "菜品是否上架（上架中：true，下架：false）", example = "true")
    private Boolean state;

    @Schema(description = "菜品在店铺中展示时的顺序", example = "1")
    private Integer sort;

    @Schema(description = "是否为必选项", example = "false")
    private Boolean required;

    public FoodVO(Food food) {
        this.foodId = food.getId();
        this.foodName = food.getName();
        this.foodImage = food.getImage();
        this.description = food.getDescription();
        String jsonStr = food.getSpecification() == null || food.getSpecification().isEmpty() ? "[]" : food.getSpecification();
        this.specification = JSONUtil.parseArray(jsonStr);
        this.storeId = food.getStoreId();
        this.storeName = food.getStoreName();
        this.storePrice = food.getStorePrice() / 100.0;
        this.limitCount = food.getLimitCount();
        this.foodTypeName = food.getFoodTypeName();
        // 如果菜品的状态为 FoodStatus.ACTIVE，则该字段为 true
        this.state = Objects.equals(food.getStatus(), FoodStatus.ACTIVE.getStatus());
        this.sort = food.getSortFlag();
        this.required = food.getRequired();
    }
}
